package cn.stronglink.asset.timers;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;

import cn.stronglink.asset.message.mq.topic.ReaderInfoSender;
import cn.stronglink.asset.message.mq.topic.entity.Collect24gSender;
import cn.stronglink.asset.message.mq.topic.entity.CollectInfo;
import cn.stronglink.asset.model.ReaderInfo;
import cn.stronglink.asset.module.patrol.service.PatrolPlanService;
import cn.stronglink.asset.module.readerInfo.service.ReaderInfoService;
import cn.stronglink.suite.core.util.ContextUtils;

public class TaskJob {
	private ReaderInfoSender topicSender = (ReaderInfoSender) ContextUtils.getBean("readerInfoSender");
	@Autowired
	private PatrolPlanService patrolPlanService;
	@Autowired
	private ReaderInfoService readerInfoService;
	/**
	 * 定时插入当天巡检单任务
	 * @throws ParseException 
	 */
	public void inspectTaskJob() throws ParseException {
		patrolPlanService.qryInspectToday();
	}
	
	/**
	 * 发送采集指令
	 */
	public void senderReader(){
		String json = this.getSenderInfo();
		topicSender.send("serverReader", json);
	}

	private String getSenderInfo() {
		List<ReaderInfo> list = readerInfoService.getSenderInfo();
		List<CollectInfo> resultList = new ArrayList<CollectInfo>();
		if(list!=null&&list.size()>0) {
			for(ReaderInfo info:list) {
				Integer readerTime = info.getReaderTime();
				String ip =info.getIp();
				Map<String, Object> temp = new HashMap<String, Object>();
				temp.put("readerTime", readerTime);
				CollectInfo dataInfo = new CollectInfo();
				dataInfo.setDevIp(ip);
				dataInfo.setDevAddrCode(info.getReaderCode());
				dataInfo.setDatas(temp);
				resultList.add(dataInfo);
			}
		}
		Collect24gSender reader = new Collect24gSender();
		reader.setActioncode("collect002");
		reader.setAwsPostData(resultList);
		return JSON.toJSONString(reader);
	}
}
